package com.fishingwithme.android.ui.screens.map

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.zIndex
import com.fishingwithme.android.ui.theme.AppDimensions
import kotlinx.coroutines.launch

/**
 * 地图右侧操作菜单组件
 */
@Composable
fun SideBar(
    modifier: Modifier = Modifier,
    barItems: List<SideBarItem>,
    onItemClick: ((EventHandler) -> Unit)? = null
) {
    val displayItems = remember { mutableStateListOf<SideBarItem>() }
    val selectedItem = remember { mutableStateOf<EventHandler?>(null) }
    val coroutineScope = rememberCoroutineScope()

    // 初始化菜单项
    androidx.compose.runtime.SideEffect {
        displayItems.clear()
        displayItems.addAll(barItems)
    }

    Box(
        modifier = modifier
            .fillMaxSize()
            .zIndex(10000.0f)
    ) {
        Box(
            modifier = Modifier

                .align(Alignment.BottomEnd)
                .padding(AppDimensions.SPACING_16)
                .padding(bottom = AppDimensions.SPACING_16) // 额外的底部padding，确保不被系统UI遮挡
        ) {
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = androidx.compose.foundation.layout.Arrangement.spacedBy(
                    AppDimensions.SPACING_4
                )
            ) {
                displayItems.forEach {
                    Column(horizontalAlignment = Alignment.CenterHorizontally) {
                        OutlinedButton(
                            onClick = {
                                // 点击菜单项时激活处理器
                                selectedItem.value = it.handler

                                // 在协程中处理点击操作
                                coroutineScope.launch {
                                    try {
                                        // 这将自动停用其他活动的处理器
                                        if (onItemClick != null && it.handler != null) {
                                            onItemClick(it.handler)
                                            it.handler.active()
                                        }
                                    } catch (e: Exception) {
                                        // 处理异常
                                    }
                                }
                            },
                            shape = RoundedCornerShape(AppDimensions.ROUNDED_CORNER_50),
                            modifier = Modifier
                                .size(AppDimensions.ICON_SIZE_60)
                                .background(
                                    color = MaterialTheme.colorScheme.surfaceVariant,
                                    shape = RoundedCornerShape(AppDimensions.ROUNDED_CORNER_50)
                                ),
                            contentPadding = PaddingValues(AppDimensions.SPACING_2),
                            border = BorderStroke(AppDimensions.BORDER_WIDTH_0, Color.Transparent)
                        ) {
                            // 显示图标
                            Icon(
                                painter = painterResource(it.icon),
                                contentDescription = it.key,
                                modifier = Modifier.size(AppDimensions.ICON_SIZE_24)
                            )
                        }
                        // 菜单项标签

                        Text(
                            text = getLabelForHandler(it.key),
                            style = MaterialTheme.typography.bodySmall,
                            //modifier = Modifier.padding(top = AppDimensions.SPACING_4),
                            textAlign = TextAlign.Center
                        )

                    }
                    // 菜单项之间的间距
                    Box(modifier = Modifier.size(AppDimensions.SPACING_8))
                }
            }
        }
    }
}

/**
 * 根据处理器获取对应的中文标签
 */
@Composable
fun getLabelForHandler(key: String): String {
    return when (key) {
        "jumpToCurrentLocation" -> "当前位置"
        "switchMapType" -> "切换地图"
        "refresh_spots" -> "刷新"
        "switch_city" -> "切换城市"
        "show_harvest" -> "晒渔获"
        "pickupPoint" -> "取点"
        "drawPolygon" -> "多边形"
        "drawLine" -> "划线"
        "drawNavigationLine" -> "导航路线"
        "drawSpotNavLine" -> "钓点路线"
        "finish" -> "完成"
        "cleanup" -> "清除"
        "refreshSpots" -> "刷新"
        "go_spot" -> "钓点"
        "go_fishing_nav" -> "导航"
        "show_harvests" -> "看鱼获"
        "update_spot" -> "更新钓点"
        "collect_spot" -> "收藏"
        "approve" -> "审批"
        "add_spots" -> "加钓点"
        "show_maintainers" -> "加人员"
        "update_circle" -> "更新"
        else -> key
    }
}
